package aiku.numericalcontroloa.Base.Utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 马文杰
 * @version 1.0
 */

@Component
@Data
public class OSSUploaderUtils {
    //accessKeyId
    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    //accessKeySecret
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${oss.endpoint}")
    private String endpoint;

    //仓库地址
    @Value("${oss.bucketName}")
    private String bucketName;

    //bucket域名
    @Value("${oss.bucket}")
    private String bucket;

    private String imgurl;

    public String uploadFile(File file) {
        try {
            // 生成唯一的文件名
            String fileName = UUID.randomUUID().toString() + "-" + file.getName();

            // 创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 判断仓库是否存在，不存在则创建
            if (ossClient.doesBucketExist(bucketName)) {
                System.out.println("仓库地址已经存在");
            } else {
                System.out.println("仓库地址" + bucketName + "不存在,创建新的仓库");
                CreateBucketRequest bucketRequest = new CreateBucketRequest(null);
                bucketRequest.setBucketName(bucketName);
                bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(bucketRequest);
            }

            // 上传文件到OSS
            try (FileInputStream inputStream = new FileInputStream(file)) {
                ossClient.putObject(bucketName, "kris/" + fileName, inputStream);

                // 关闭输入流
                inputStream.close();
            }

            // 关闭OSSClient
            ossClient.shutdown();

            // 返回上传后的文件URL
            String imgUrl = "https://" + bucketName + "." + endpoint + "/kris/" + fileName;
            return imgUrl;
        } catch (IOException e) {
            e.printStackTrace();
            // 处理上传失败的情况
            return null;
        }
    }

    public String uploadFile(MultipartFile file) {
        try {
            // 生成唯一的文件名
            String fileName = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();

            // 创建OSSClient实例
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //判断仓库是否存在，不存在测创建
            if (ossClient.doesBucketExist(bucketName)){
                System.out.println("仓库地址已经存在");
            }else{
                System.out.println("仓库地址"+bucketName+"不存在,创建新的仓库");
                CreateBucketRequest bucketRequest = new CreateBucketRequest(null);
                bucketRequest.setBucketName(bucketName);
                bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(bucketRequest);
            }

            // 上传文件到OSS
            ossClient.putObject(bucketName, "kris/"+fileName, file.getInputStream());

            // 关闭OSSClient
            ossClient.shutdown();

            // 返回上传后的文件URL
            imgurl = "https://" + bucketName + "." + endpoint + "/kris/" + fileName;
            return imgurl;
        } catch (IOException e) {
            e.printStackTrace();
            // 处理上传失败的情况
            return null;
        }
    }
}
